# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from datetime import datetime

from openerp.osv import fields, osv
from openerp.tools.translate import _

class switch_customer_refund_no(osv.osv):
    _name = 'switch.customer.refund.no'
    _description = 'Switch Customer Refund No.'
    
    _columns = {
        'cust_refund1':fields.many2one('account.invoice', '1st Customer Refund No.', required=True),
        'cust_refund2':fields.many2one('account.invoice', '2nd Customer Refund No.', required=True),
     }
    
    def switch_no(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        
        refund_obj = self.pool.get('account.invoice')
        move_obj = self.pool.get('account.move')
        move_line_obj = self.pool.get('account.move.line')
        voucher_line_obj = self.pool.get('account.voucher.line')
        
        for ref in self.browse(cr, uid, ids, context=context):
            if not ids:
                return []

            refund1 = ref.cust_refund1.internal_number or ''
            refund2 = ref.cust_refund2.internal_number or ''
            
            if refund1 == refund2:
                raise osv.except_osv(_('Warning!'), _('You cannot switch between customer refund no that have same payment no'))
            if ref.cust_refund1.journal_id.id != ref.cust_refund2.journal_id.id:
                raise osv.except_osv(_('Warning!'), _('You cannot switch between customer refund no that have different journal id'))
            
            #Update into dummy
            invoice_payment_ids = voucher_line_obj.search(cr, uid, [('name', '=', refund1)])
            for inv_pay in voucher_line_obj.browse(cr, uid, invoice_payment_ids):
                if inv_pay.id:
                    voucher_line_obj.write(cr, uid, inv_pay.id, {'name': refund2 + '-dummy'}, context=context)
                    for inv_ml in inv_pay.voucher_id.move_id.line_id:
                        if inv_ml.name == invoice1:
                            move_line_obj.write(cr, uid, inv_ml.id, {'name': refund2}, context=context)

            invoice_payment_ids2 = voucher_line_obj.search(cr, uid, [('name', '=', refund2)])
            for inv_pay2 in voucher_line_obj.browse(cr, uid, invoice_payment_ids2):
                if inv_pay2.id:
                    voucher_line_obj.write(cr, uid, inv_pay2.id, {'name': refund1 + '-dummy'}, context=context)
                    for inv_ml2 in inv_pay2.voucher_id.move_id.line_id:
                        if inv_ml2.name == invoice2:
                            move_line_obj.write(cr, uid, inv_ml2.id, {'name': refund1}, context=context)
            
            #Update back real invoice no
            invoice_payment_ids = voucher_line_obj.search(cr, uid, [('name', '=', refund2 + '-dummy')])
            for inv_pay in voucher_line_obj.browse(cr, uid, invoice_payment_ids):
                if inv_pay.id:
                    voucher_line_obj.write(cr, uid, inv_pay.id, {'name': refund2}, context=context)
            
            invoice_payment_ids2 = voucher_line_obj.search(cr, uid, [('name', '=', refund1 + '-dummy')])
            for inv_pay2 in voucher_line_obj.browse(cr, uid, invoice_payment_ids2):
                if inv_pay2.id:
                    voucher_line_obj.write(cr, uid, inv_pay2.id, {'name': refund1}, context=context)
                          
            #dummy
            if ref.cust_refund1.move_id:
                move_obj.write(cr, uid, ref.cust_refund1.move_id.id, {'name': refund2 + '-dummy', 'ref': refund2 + '-dummy'}, context=context)
                for ref1_move_line in ref.cust_refund1.move_id.line_id:
                    move_line_obj.write(cr, uid, ref1_move_line.id, {'ref': refund2 + '-dummy'}, context=context) 
            refund_obj.write(cr, uid, ref.cust_refund1.id, {'internal_number': refund2 + '-dummy'}, context=context)
            
            if ref.cust_refund2.move_id:
                move_obj.write(cr, uid, ref.cust_refund2.move_id.id, {'name': refund1 + '-dummy', 'ref': refund1 + '-dummy'}, context=context)
                for ref2_move_line in ref.cust_refund2.move_id.line_id:
                    move_line_obj.write(cr, uid, ref2_move_line.id, {'ref': refund1 + '-dummy'}, context=context)
            refund_obj.write(cr, uid, ref.cust_refund2.id, {'internal_number': refund1 + '-dummy'}, context=context)
            
            #real
            if ref.cust_refund1.move_id:
                move_obj.write(cr, uid, ref.cust_refund1.move_id.id, {'name': refund2, 'ref': refund2}, context=context)
                for ref1_move_line in ref.cust_refund1.move_id.line_id:
                    move_line_obj.write(cr, uid, ref1_move_line.id, {'ref': refund2}, context=context) 
            refund_obj.write(cr, uid, ref.cust_refund1.id, {'internal_number': refund2}, context=context)
            
            if ref.cust_refund2.move_id:
                move_obj.write(cr, uid, ref.cust_refund2.move_id.id, {'name': refund1, 'ref': refund1}, context=context)
                for ref2_move_line in ref.cust_refund2.move_id.line_id:
                    move_line_obj.write(cr, uid, ref2_move_line.id, {'ref': refund1}, context=context)
            refund_obj.write(cr, uid, ref.cust_refund2.id, {'internal_number': refund1}, context=context)
        
        return True
    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
